package code.nest.dto;

import code.nest.proto.LogProto;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author ：Hyman
 * @date ：Created in 2021/6/22 16:41
 * @description：
 * @modified By：
 * @version: $
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Log implements Serializable, Protobufable {

    private static final long serialVersionUID = 4681549852646984537L;

    private int type;
    private String content;
    private long recordTime;

    public Log(byte[] bytes) {
        try {
            LogProto.Log log = LogProto.Log.parseFrom(bytes);
            this.type = log.getType();
            this.content = log.getContent();
            this.recordTime = log.getRecordTime();
        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        }

    }

    @Override
    public byte[] encode() {
        LogProto.Log.Builder builder = LogProto.Log.newBuilder();
        builder.setType(type);
        builder.setContent(content);
        builder.setRecordTime(recordTime);
        return builder.build().toByteArray();
    }

    @Override
    public String toString() {
        return "[类型:" + type + ", 内容：" + content + ", 记录时间：" + recordTime + "]";
    }
}
